package mnstudent.global;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author Administrator
 */
public class ConnectionsHandler extends DefaultHandler {

    private List<Connection> lstConnections;
    private Connection connection;

    private boolean aURL;
    private boolean aDriverName;
    private boolean aHost;
    private boolean aPort;
    private boolean aIntegratedSecurity;
    private boolean aUserName;
    private boolean aPassword;
    private boolean aDatabaseName;
    private boolean aOthers;

    public ConnectionsHandler() {
        lstConnections = null;
        connection = null;

        aURL = false;
        aDriverName = false;
        aHost = false;
        aPort = false;
        aIntegratedSecurity = false;
        aUserName = false;
        aPassword = false;
        aDatabaseName = false;
        aOthers = false;
    }

    public List<Connection> getLstConnections() {
        return lstConnections;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("connection")) {
            String id = attributes.getValue("id");
            connection = new Connection();
            connection.setId(Integer.parseInt(id));

            if (lstConnections == null) {
                lstConnections = new ArrayList<>();
            }
        } else if (qName.equalsIgnoreCase("url")) {
            aURL = true;
        } else if (qName.equalsIgnoreCase("drivername")) {
            aDriverName = true;
        } else if (qName.equalsIgnoreCase("host")) {
            aHost = true;
        } else if (qName.equalsIgnoreCase("port")) {
            aPort = true;
        } else if (qName.equalsIgnoreCase("isintegratedsecurity")) {
            aIntegratedSecurity = true;
        } else if (qName.equalsIgnoreCase("username")) {
            aUserName = true;
        } else if (qName.equalsIgnoreCase("password")) {
            aPassword = true;
        } else if (qName.equalsIgnoreCase("others")) {
            aOthers = true;
        } else if (qName.equalsIgnoreCase("databasename")) {
            aDatabaseName = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("connection")) {
            lstConnections.add(connection);
        }
    }

    @Override
    public void characters(char ch[], int start, int lenght) throws SAXException {
        if (aURL) {
            connection.setURL(new String(ch, start, lenght));
            aURL = false;
        } else if (aDriverName) {
            connection.setDriverName(new String(ch, start, lenght));
            aDriverName = false;
        } else if (aHost) {
            connection.setHost(new String(ch, start, lenght));
            aHost = false;
        } else if (aPort) {
            connection.setPort(new String(ch, start, lenght));
            aPort = false;
        } else if (aIntegratedSecurity) {
            connection.setIntegratedSecurity(Boolean.parseBoolean(new String(ch, start, lenght)));
            aIntegratedSecurity = false;
        } else if (aUserName) {
            connection.setUserName(new String(ch, start, lenght));
            aUserName = false;
        } else if (aPassword) {
            connection.setPassword(new String(ch, start, lenght));
            aPassword = false;
        } else if (aOthers) {
            connection.setOthers(new String(ch, start, lenght));
            aOthers = false;
        } else if (aDatabaseName) {
            connection.setDatabaseName(new String(ch, start, lenght));
            aDatabaseName = false;
        }
    }
}
